package com.lyw.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyw.gulimall.product.vo.SpuSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyw.gulimall.product.entity.SpuInfoEntity;
import com.lyw.gulimall.product.service.SpuInfoService;
import com.lyw.common.utils.R;



/**
 * spu信息
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2022-05-31 11:35:50
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;


    // /product/spuinfo/{spuId}/up
    /**
     * 商品上架功能
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId){
        spuInfoService.up(spuId);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:spuinfo:list")
    public R list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                  @RequestParam(value = "sidx", required = false) String sidx,
                  @RequestParam(value = "order", required = false) String order,
                  @RequestParam(value = "key", required = false) String key,
                  @RequestParam(value = "catelogId", required = false) Long catelogId,
                  @RequestParam(value = "brandId", required = false) Long brandId,
                  @RequestParam(value = "status", required = false) String status){
        /*
        {
           page: 1,//当前页码
           limit: 10,//每页记录数
           sidx: 'id',//排序字段
           order: 'asc/desc',//排序方式
           key: '华为',//检索关键字
           catelogId: 6,//三级分类id
           brandId: 1,//品牌id
           status: 0,//商品状态
        }
         */
        IPage<Map<String, Object>> list = spuInfoService.queryPageByCondition(page, limit, sidx, order, key, catelogId, brandId, status);

        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVo vo){
        spuInfoService.saveSpuInfo(vo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
